package net.mlike.openssp.server.security.shiro;

import net.mlike.openssp.server.security.shiro.token.JwtToken;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.xml.bind.DatatypeConverter;

/**
 * Created by wallellen@hotmail.com on 5/31/18.
 */
public class JwtRealm extends AuthorizingRealm {
    private static final String SECRET = Base64.encodeBase64String("Hello world".getBytes());

    @Override
    public Class getAuthenticationTokenClass() {
        return JwtToken.class;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authToken) throws AuthenticationException {
        if (!(authToken instanceof JwtToken)) {
            return null;
        }

        JwtToken token = (JwtToken) authToken;
        String tokenStr = (String) token.getCredentials();
        byte[] bytes = DatatypeConverter.parseBase64Binary(SECRET);

        return null;
    }
}
